package org.sang.controller.mbr;

import org.sang.bean.RechargeRecord;
import org.sang.bean.RespBean;
import org.sang.service.RechargeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

import static org.springframework.http.ResponseEntity.badRequest;
import static org.springframework.http.ResponseEntity.ok;

/**
 * Created by sang on 2018/1/12.
 */
@RestController
@RequestMapping("/mbr/card/recharge/record/basic")
public class RechargeRecordBasicController {
    @Autowired
    RechargeRecordService rechargeRecordService;

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public RespBean addRechargeRecord(@RequestBody RechargeRecord rechargeRecord) {
        if (rechargeRecordService.addRechargeRecord(rechargeRecord) == 1) {
            return new RespBean("success", "保存成功!");
        }
        return new RespBean("error", "保存失败!");
    }


    @RequestMapping(value = "/", method = RequestMethod.PUT)
    public RespBean updateRechargeRecord(@RequestBody RechargeRecord rechargeRecord) {
        if (rechargeRecordService.updateRechargeRecord(rechargeRecord) == 1) {
            return new RespBean("success", "更新成功!");
        }
        return new RespBean("error", "更新失败!");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public RespBean deleteRechargeRecord(@PathVariable(name = "id") String id) {
        if (rechargeRecordService.deleteRechargeRecord(id) == 1) {
            return new RespBean("success", "删除成功!");
        }
        return new RespBean("error", "删除失败!");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Object queryRechargeRecord(@PathVariable(name = "id") String id) {
        RechargeRecord rechargeRecord = rechargeRecordService.queryRechargeRecord(id);
        if (Objects.isNull(rechargeRecord)) {
            return badRequest().body(new RespBean("error", "资源不存在!"));
        }
        return ok(rechargeRecord);
    }

    @RequestMapping(value = "/collection", method = RequestMethod.GET)
    public Object queryRechargeRecords(RechargeRecord rechargeRecord) {
        List<RechargeRecord> rechargeRecords = rechargeRecordService.queryRechargeRecords(rechargeRecord);
        return ok(rechargeRecords);
    }

}
